package com.zx.baofu.alcohol.service.impl;

import com.zx.baofu.alcohol.mapper.OrderMapper;
import com.zx.baofu.alcohol.model.LotteryProbability;
import com.zx.baofu.alcohol.model.Result;
import com.zx.baofu.alcohol.model.UserOrder;
import com.zx.baofu.alcohol.service.OrderService;
import com.zx.baofu.alcohol.util.MyStringUtil;
import com.zx.baofu.alcohol.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@Component
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Result userWriteAddress(String unionid, String orderNum, String name, String mobile, String address, String area, String town) {
        String fullAddress = "保定市";
        if( ! MyStringUtil.isNullorEmpty(area)){
            fullAddress = fullAddress + area;
        }
        if (! MyStringUtil.isNullorEmpty(town)){
            fullAddress = fullAddress + town;
        }
        fullAddress = fullAddress + address;
        int res = orderMapper.userWriteAddress(unionid, orderNum, name, mobile, fullAddress);
        if (res <= 0){
            return new Result().fail("保存数据失败");
        }
        return new Result().success("保存信息成功");
    }

    @Override
    public Result orderList(String text, Integer claimed, Integer pageNum, Integer pageSize) {
        int start = PageUtil.caclStart(pageNum, pageSize);
        List<UserOrder> list = orderMapper.orderList(text, claimed, start, pageSize);
        if (list.size() <= 0){
            return new Result().fail("没有符合条件的数据", Collections.emptyList());
        }
        int totalNum = orderMapper.countOrderList(text, claimed);
        int totalPage = PageUtil.caclTotalPage(pageSize, totalNum);
        return new Result().success("查询成功", list, totalNum, totalPage);
    }

    @Override
    public Result claimOrder(Integer orderId) {
        int res = orderMapper.claimOrder(orderId);
        if (res <= 0){
            return new Result().fail("核销失败");
        }
        return new Result().success("核销成功");
    }

    @Override
    public Result getProbability() {
        int probability = orderMapper.getProbability();
        return new Result().success("查询成功", probability);
    }

    @Override
    public Result updateProbability(List<LotteryProbability> probabilitys) {
        int res = orderMapper.updateProbability(probabilitys);
        if (res <= 0){
            return new Result().fail("修改失败");
        }
        return new Result().success("修改成功");
    }

    @Override
    public Result userOrder(String unionid) {
        UserOrder userOrder = orderMapper.userOrder(unionid);
        if (userOrder == null){
            return new Result().fail("您当前没有订单");
        }
        return new Result().success("查询成功", userOrder);
    }

    @Override
    public Result getAllLottery() {
        List<LotteryProbability> lists=orderMapper.getAllLottery();
        return new Result().success("查询成功",lists);
    }

    @Override
    public Result getUserOrder(Long id) {
        UserOrder order=orderMapper.getUserOrder(id);
        return new Result().success("查询成功",order);
    }


}
